<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog :title="dialogText" :visible="showDialog" @close="$emit('update:showDialog', false)">
    <!-- 表单 -->
    <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addPermission, getPermissionDetail } from '@/api/permission'
export default {
  props: {
    showDialog: {
      type: Boolean
    },
    dialogText: {
      type: [Number, String],
      required: true
    },
    dialogPid: {
      type: [Number, String],
      required: true
    },
    dialogType: {
      type: [Number, String],
      required: true
    }

  },
  data() {
    return {

      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },

  methods: {

    btnOK() {
      this.$refs.formData.validate((val) => {
        this.formData.pid = this.dialogPid
        this.formData.type = this.dialogType
        if (val) {
          this.upPermission()
        }
      })
    },
    async upPermission() {
      await addPermission(this.formData)
      this.$message.success('添加成功')
      this.btnCancel()
    },
    async upPermissionDetail() {
      const res = await getPermissionDetail(this.dialogPid)
      console.log(res)
      this.formData = res.data
    },
    btnCancel() {
      this.$emit('update')
      this.$emit('update:showDialog', false)
    },
    formDel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      }
    }
  }
}
</script>

<style>

</style>
